package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.shop.MallShopActivity;
import com.sinoglobal.ningxia.activity.shop.ShoppingCashActivity;
import com.sinoglobal.ningxia.activity.shop.ShoppingScoreActivity;
import com.sinoglobal.ningxia.beans.ShallShopIndexDataVo;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.EquipmentUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

/**
 * @author zbl
 * @createdate 2014-6-24 上午10:38:36
 * @Description:精品商城中商品的adapter
 */
public class ShoppingGridAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ArrayList<ShallShopIndexDataVo> data1 = new ArrayList<ShallShopIndexDataVo>();
	private Context context;
	private String type;
	private Intent intent;

	public ShoppingGridAdapter(Context context, ArrayList<ShallShopIndexDataVo> data1, String type) {
		super();
		this.data1 = data1;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.type = type;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setData(ArrayList<ShallShopIndexDataVo> data1) {
		this.data1 = data1;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return data1.size();
	}

	@Override
	public Object getItem(int position) {
		return data1.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final String CASH_TYPE="1";//现金购买
		final String SCORE_TYPE="2";//积分购买
		ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.mallshop_viewpager_grid_item, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.shopping_grid_itemImg);
			holder.cash = (TextView) convertView.findViewById(R.id.item_cash);
			holder.cashType = (TextView) convertView.findViewById(R.id.item_typeCash);
			holder.itemscore = (TextView) convertView.findViewById(R.id.item_score);
			holder.scoreType = (TextView) convertView.findViewById(R.id.item_type_score);
			holder.tv2 = (TextView) convertView.findViewById(R.id.item_goodsName);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		FinalBitmap fb = FinalBitmap.create(context);
		fb.configLoadfailImage(R.drawable.default43);
		fb.configLoadingImage(R.drawable.default43);
		String URL = ConnectionUtil.localUrl + data1.get(position).getGoods_img();
		// 设置图片的宽高
		int width = (FlyApplication.widthPixels - EquipmentUtil.dip2px(context, 45)) / 2;
		int height = 3 * width / 4;
		holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
		// fb.display(holder.iv, URL);
		fb.display(holder.iv, URL, width, height);
		// 判断是积分还是现金购买类型
		if (data1.get(position).getType().equals(CASH_TYPE) && type.equals(CASH_TYPE)) {
			holder.scoreType.setVisibility(View.GONE);
			holder.itemscore.setVisibility(View.GONE);
			holder.cash.setVisibility(View.VISIBLE);
			holder.cashType.setVisibility(View.VISIBLE);
			holder.cash.setText(data1.get(position).getScore());
		} else if (data1.get(position).getType().equals(SCORE_TYPE) && type.equals(SCORE_TYPE)) {
			holder.scoreType.setVisibility(View.VISIBLE);
			holder.itemscore.setVisibility(View.VISIBLE);
			holder.cash.setVisibility(View.GONE);
			holder.cashType.setVisibility(View.GONE);
			holder.itemscore.setText(data1.get(position).getScore());
		}
		holder.tv2.setText(data1.get(position).getGoods_name());

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {

				// 判断是现金购买还是积分兑换
				if (CASH_TYPE.equals(data1.get(position).getType())) {

					intent = new Intent(context, ShoppingCashActivity.class);
					intent.putExtra("goods_id", data1.get(position).getId());
				} else if (SCORE_TYPE.equals(data1.get(position).getType())) {
					intent = new Intent(context, ShoppingScoreActivity.class);
					intent.putExtra("goods_id", data1.get(position).getId());
				}
				((MallShopActivity) context).startActivityForResult(intent, 0);

			}
		};
		holder.iv.setOnClickListener(listener);
		holder.tv2.setOnClickListener(listener);
		return convertView;
	}

	class ViewHolder {
		ImageView iv;
		TextView cashType;// 积分
		TextView cash;// 积分
		TextView scoreType;// 积分
		TextView itemscore;// 积分
		TextView tv2;// 名称

	}
}
